var P7 = function (game) {
    this.create = function () {

        ClosePage(game, '7_0', '7', '7_r', '7_w');


        function Page(game, cbg, mbg, rbg, wbg) {
            var tar = [];
            var bg = game.add.sprite(0, 0, mbg);
            //元素位置的计算原点
            bg.anchor.setTo(0, 0);

            bounds = new Phaser.Rectangle(240, 160, 1146, 450);

            var graphics = game.add.graphics(bounds.x, bounds.y);
            graphics.beginFill(0x000077);
            graphics.drawRect(0, 0, bounds.width, bounds.height);
            graphics.visible = false;


            var group = game.add.group();

            var bimgs = ['d7_0', 'd7_1', 'd7_3', 'd7_4'];
            var posXO = [840, 1060,390, 620];
            var posX = [390, 620, 840, 1060,];
            var posY = 270;
            var word = ['敲打', '夯压', '旋转', '试粉'];
            var style = {
                font: "16px",
                fill: "#1666A5",
                align: "center",
                wordWrap: true,
                wordWrapWidth: 20
            };
            var tween,button;

            for (var i = 0; i < 4; i++) {
                button = game.add.sprite(posXO[i], 270, '');


                var sprite=game.make.sprite(0, 0, bimgs[i]);
                sprite.animations.add('run');
                sprite.animations.play('run', 6, true);
                button.addChild(sprite);
                text = game.make.text(0, 200, word[i], style);
                text.x = (160 - text.width) / 2;
                button.addChild(text);

                button.name = bimgs[i];
                button.data = i;
                button.inputEnabled = true;
                button.input.useHandCursor = true;
                button.input.enableDrag(false, true);
                button.input.boundsRect = bounds;

                group.add(button);
                //button.input.enableSnap(90, 90, false, true);

                button.events.onDragStop.add(fixLocation);
            }

            function fixLocation() {
                var xary = [];
                group.children.forEach(function (item) {
                    xary.push(item.x);
                });

                tar = getSortList(xary);
                group.children.forEach(function (item, i) {
                    tween = game.add.tween(item).to({x: posX[tar[i]], y: posY}, 500, Phaser.Easing.Linear.None, true);
                });
            }

            var getSortList = function (ary) {
                var target = [],
                    newli = [];
                ary.forEach(function (v, i) {
                    newli[i] = v;
                });
                newli = newli.sort(function (a, b) {
                    return a - b;
                });
                ary.forEach(function (v, i) {
                    newli.forEach(function (val, k) {
                        if (v === val) {
                            target[i] = k;
                        }
                    });
                });
                return target;
            };
            var retry = game.add.button(1080, 625, imgs['button'], onSubmit, this, 2, 1, 0);
            retry.name = 'retry';
            retry.width = 160;
            retry.height = 55;
            //container.addChild(retry);

            var submit = game.add.button(1250, 625, imgs['button'], onSubmit, this, 2, 1, 0);
            submit.name = 'submit';
            submit.width = 160;
            submit.height = 55;

            //container.addChild(submit);

            function onSubmit(arg) {
                if (arg.name === 'submit') {
                    var flag=true;
                    for(var i=0;i<4;i++){
                        var obj=group.getByName(bimgs[i]);
                        if(obj.x!=posX[i]){
                            flag=false;
                        }
                    }
                    if (flag) {
                        RightPage(game, cbg, mbg, rbg, wbg);
                    } else {
                        WrongPage(game, cbg, mbg, rbg, wbg);
                    }
                    group.destroy();
                } else {
                    game.goState('back');
                }
            }
        }


        function ClosePage(game, cbg, mbg, rbg, wbg) {
            var container = game.add.sprite(0, 0, '');
            var close = game.add.button(0, 0, cbg, onClose, this, 2, 1, 0);
            container.addChild(close);

            function onClose() {
                container.destroy();
                Page(game, cbg, mbg, rbg, wbg);
            }
        }

        function WrongPage(game,cbg,mbg,rbg,wbg){
            var container=game.add.sprite(0,0,'');

            var bg = game.add.image(0, 0, wbg);
            container.addChild(bg);
            var retry = game.add.button(1250, 625, imgs['button'], onClick, this, 2, 1, 0);
            retry.width = 160;
            retry.height = 55;
            container.addChild(retry);

            function onClick() {
                container.destroy();
                Page(game, cbg, mbg, rbg, wbg);
            }
        }

        function RightPage(game, cbg, mbg, rbg, wbg) {
            var container = game.add.sprite(0, 0, '');

            var bg = game.add.image(0, 0, rbg);
            container.addChild(bg);
            var next = game.add.button(1250, 625, imgs['button'], onClick, game, 2, 1, 0);
            next.width = 160;
            next.height = 55;
            container.addChild(next);

            function onClick() {
                game.goState('next');
            }
        }

    };

}